//第二站常规化
//创建一个
function choose(val) {
    var arr = new Array(3);
    arr[0] = ["穴位定位", "儿科穴位", "成人穴位"];
    arr[1] = ["单项手法", "成人手法", "儿科手法"];
    arr[2] = ["整套手法", "伤科", "妇科", "儿科", "内科"];
    // 底层分类的select
    var nomalBottomCategory = document.getElementById("nomalBottomCategory");
    // 获取option
    var nomalSelectCategoryOption = nomalBottomCategory
        .getElementsByTagName("option");
    // 设置可操作
    nomalBottomCategory.disabled = false;
    // 先删除，后添加
    for (var i = 0; i < nomalSelectCategoryOption.length; i++) {
        var op = nomalSelectCategoryOption[i];
        // 删除option
        nomalBottomCategory.removeChild(op);
        // 数组长度发生变化，需处理
        i--;
    }

    // 遍历
    for (var i = 0; i < arr.length; i++) {
        // 取一维数组
        var arr1 = arr[i];
        // 取一维数组的第一个值
        var firstVal = arr1[0];
        // 判断
        if (firstVal == val) {
            // 遍历
            for (var j = 1; j < arr1.length; j++) {
                // 获取城市名
                var value = arr1[j];
                // 创建option
                var optionl = document.createElement("option");
                // 创建文本
                var textl = document.createTextNode(value);
                // 把文本添加到标签
                optionl.appendChild(textl);
                // 添加到city里面
                nomalBottomCategory.appendChild(optionl);
            }
        }
    }
}

//第二站客观化
//第二站客观化
function linkageThree(val) {
    var arr = new Array(2);
    arr[0] = ["穴位定位", "empty"];
    arr[1] = ["单项手法", "挤压类", "摆动类", "叩击类", "振动类", "摩擦类"];
    // 底层分类的select
    var categoryThreeUseful = document.getElementById("categoryThreeUseful");
    // 获取option
    var selectCategoryOption = categoryThreeUseful.getElementsByTagName("option");
    // 设置可操作
    categoryThreeUseful.disabled = false;
    // 先删除，后添加
    for (var i = 0; i < selectCategoryOption.length; i++) {
        var op = selectCategoryOption[i];
        // 删除option
        categoryThreeUseful.removeChild(op);
        // 数组长度发生变化，需处理
        i--;
    }

    // 遍历
    for (var i = 0; i < arr.length; i++) {
        // 取一维数组
        var arr1 = arr[i];
        // 取一维数组的第一个值
        var firstVal = arr1[0];
        // 判断
        if (firstVal == val) {
            // 遍历
            for (var j = 1; j < arr1.length; j++) {
                // 获取城市名
                var value = arr1[j];
                if (value == 'empty') {
                    $("#categoryThreeUseful").hide();
                } else {
                    $("#categoryThreeUseful").show();
                }
                // 创建option
                var optionl = document.createElement("option");
                // 创建文本
                var textl = document.createTextNode(value);
                // 把文本添加到标签
                optionl.appendChild(textl);
                // 添加到city里面
                categoryThreeUseful.appendChild(optionl);
            }
        }
    }
}

$(document).ready(function () {
    var categoryThreeUseful = $("#categoryThreeUseful").val();
    if (categoryThreeUseful == 'empty') {
        $("#categoryThreeUseful").hide();
    }
});

/*第一站问答操作提醒*/
function importExaminationOne() {
    var examinationsFile = $("#importExaminationOne input[name='examinationsFile']").val();
    var fileFormat = examinationsFile.substr(examinationsFile.lastIndexOf('.'));
    var isEmpty = isEmptyFile(examinationsFile, fileFormat);
    if (isEmpty == false) {
        return false;
    }


    var formData = new FormData($('#importExaminationOne')[0]);
    /* formData.append('num', '1'); *///可以在已有表单数据的基础上，继续添加新的键值对
    examImportSubmit(formData, basePath.value + '/examination/fileUpload');
}

/*第二站常规化操作提醒*/
function importExaminationTwo() {
    var examinationsFile = $("#importExaminationTwo input[name='examinationsFile']").val();
    var fileFormat = examinationsFile.substr(examinationsFile.lastIndexOf('.'));
    var isEmpty = isEmptyFile(examinationsFile, fileFormat);
    if (isEmpty == false) {
        return false;
    }

    var formData = new FormData($('#importExaminationTwo')[0]);
    examImportSubmit(formData, basePath.value + '/examination/fileUpload');
}

/*第二站客观化化操作提醒*/
function importExaminationThree() {
    var examinationsFile = $("#importExaminationThree input[name='examinationsFile']").val();
    var fileFormat = examinationsFile.substr(examinationsFile.lastIndexOf('.'));
    var isEmpty = isEmptyFile(examinationsFile, fileFormat);
    if (isEmpty == false) {
        return false;
    }

    var formData = new FormData($('#importExaminationThree')[0]);
    examImportSubmit(formData, basePath.value + '/examination/fileUpload');
}

function isEmptyFile(examinationsFile, fileFormat) {
    var flag = true;
    if (examinationsFile == null || examinationsFile == "") {
        layer.confirm('请选择文件！', {
            btn: ["确认"]
        });
        flag = false;
    } else if (fileFormat != '.docx') {
        layer.confirm('所选文件格式不正确！', {
            btn: ["确认"]
        });
        flag = false;
    }
    return flag;
}

/**
 *@Author: 陈玉林
 *@Description: 返回的状态码以及结果处理
 *@Date: 2017/8/10 21:59
 */

function noticeAfterUpload(result) {
    var code = result.code;
    var msg = result.msg;
    var data = result.data;
    if (code == 200) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 0) {
        layer.msg('成功导入' + data + '道试题！', {
            icon: 1,
            time: 4000
        });
    } else if (code == 103) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    }else if (code == 101) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    }else if (code == 104) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    }else if (code == 105) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    }else if (code == 106) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 107) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 108) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 102) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 2) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    } else if (code == 100) {
        layer.msg(msg, {
            icon: 2,
            time: 4000
        });
    }

}

/**
 * form提交
 * @param formData
 * @param url
 */
function examImportSubmit(formData, url) {
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: formData,
        dataType:"json",
        processData: false,
        contentType: false
    }).done(function (result) {
        noticeAfterUpload(result);
    }).fail(function (res) {
    });
}